package com.dap.core.context;

import java.io.IOException;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import com.dap.core.config.BootstrapConfigUtil;
import com.dap.core.utils.IOUtils;
import com.dap.core.utils.StringUtils;

public class ContextUtils {
    public static final Logger LOGGER = LoggerFactory.getLogger(BootstrapConfigUtil.class);
    private static final DapContext DAP_CONTEXT = new DapContext();
    private static ApplicationContext SPRING_DAP_CONTEXT;
    private static ApplicationContext SPRING_APP_CONTEXT;
    private static boolean isApplicationStarted = false;


    static {
        configDubbo();
    }


    private static void configDubbo() {
        System.setProperty("dubbo.application.logger", "slf4j");

        final String rpcPropsFile = "config/system.properties";
        final String setPropertiesPrefix = "rpc.";

        try {
            Resource resource = new ClassPathResource(rpcPropsFile);
            Properties props = IOUtils.stream2props(resource.getInputStream());
            for (String key : props.stringPropertyNames()) {
                if (key.startsWith(setPropertiesPrefix) && !key.endsWith(".group") && !key.endsWith(".address")) {
                    String dubboKey = "dubbo." + key.substring(setPropertiesPrefix.length());
                    String value = props.getProperty(key);

                    value = StringUtils.replaceProperty(value, BootstrapConfigUtil.getProperties());
                    LOGGER.info("Setting system property value for dubbo, key=" + dubboKey + " ,value=" + value);
                    System.setProperty(dubboKey, value);
                }
            }
        } catch (IOException e) {
            LOGGER.warn("Failed to load rpc config properties from " + rpcPropsFile + ": " + e.getMessage(), e);
        }
    }



    public static DapContext getDapContext() {
        return DAP_CONTEXT;
    }


    public static ApplicationContext getSpringDapContext() {
        return SPRING_DAP_CONTEXT;
    }


    public static void setSpringDapContext(ApplicationContext springDapContext) {
        SPRING_DAP_CONTEXT = springDapContext;
    }


    public static ApplicationContext getSpringAppContext() {
        return SPRING_APP_CONTEXT;
    }


    public static void setSpringAppContext(ApplicationContext springAppContext) {
        SPRING_APP_CONTEXT = springAppContext;
    }


    public static String getTenantId() {
        return BootstrapConfigUtil.getProperty(BootstrapConfigUtil.APP_TENANTID);
    }


    public static String getConfigProperty(String property) {
        String value = System.getenv(property);
        if (StringUtils.isEmpty(value)) {
            value = getDapContext().configuration.getProperty(property);
        }
        if (StringUtils.isEmpty(value)) {
            value = BootstrapConfigUtil.getProperty(property);
        }
        return StringUtils.replaceProperty(value, getDapContext().configuration);
    }


    public static boolean isApplicationStarted() {
        return isApplicationStarted;
    }


    public static void setApplicationStarted(boolean isApplicationStarted) {
        ContextUtils.isApplicationStarted = isApplicationStarted;
    }
}
